package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.BatchProducts;
import com.qingxin.park.entity.BatchProductsReport;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFile;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;

/**
 * 产品检测报告
 * @author Administrator
 *
 */
@Service
public class BatchProductsReportService extends BaseService<BatchProductsReport, String> {


/**
	 * 获取检测报告
	 * @param pdtid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BatchProductsReport> listProductsReport(String pdtid){
		List<BatchProductsReport> list = listByHql("from BatchProductsReport p where p.batchProducts.pdtid=?", pdtid);
		if(list == null){
			return new ArrayList<>();
		}
		return list;
	}
	
	
	/**
	 * 添加检测报告
	 * @param pdtid
	 * @param uploadFileParam
	 * @param loginUser
	 * @return
	 */
	public SysError add(String pdtid, MyUploadFileParam uploadFileParam, LoginUser loginUser){
		
		BatchProducts products = new BatchProducts();
		products.setPdtid(pdtid);
		
		//删除
		if(uploadFileParam.getDeleteNum() > 0){
			String[] ids = uploadFileParam.getDeleteIds();
			for(String id : ids){
				BatchProductsReport report = get(id);
				this.delete(report);
			}
		}
		
		//修改
		int modifyNum = uploadFileParam.getModifyNum();
		if(modifyNum > 0){
			for(int i=0; i<modifyNum; i++){
				MyUploadFile uploadFile = uploadFileParam.getModify(i);
				BatchProductsReport report = getSafeEntity(uploadFile.getFileId());
				if(report == null){
					continue;
				}
				report.setReportName(uploadFile.getFileDesc());
				Updater<BatchProductsReport> updater = new Updater<>(report, "reportName");
				this.update(updater);
			}
		}
		
		//添加
		int uploadNum = uploadFileParam.getUploadNum();
		if(uploadNum > 0){
			for(int i=0; i<uploadNum; i++){
				MyUploadFile uploadFile = uploadFileParam.getUpload(i);
				//从临时目录移动到保存目录
				UploadFile.moveTmpTo(uploadFile.getFileName(), UploadFile.DIR_PRODUCTS_IMG);
				
				BatchProductsReport report = new BatchProductsReport(products, uploadFile.getFileDesc(),
						uploadFile.getFileName(), loginUser.getLogin_name(), new Date(), loginUser.getLogin_name(), new Date());
				this.save(report);
			}
		}
		
		return null;
	}


	
}
